import json
from utils.models import build_model

class Task:
    def __init__(self,name,dataset,model,preprocessor,conf):
        self.name=name
        self.ds=dataset
        self.mdl=model
        self.pp=preprocessor
        with open('jsons/tasks/'+name+'.json',encoding='utf8') as f:
            self.content=json.load(f)
        with open('jsons/datasets/'+dataset+'.json',encoding='utf8') as f:
            self.dataset=json.load(f)
        with open('jsons/models/'+model+'.json',encoding='utf8') as f:
            self.model=json.load(f)
        with open('jsons/preprocessors/'+preprocessor+'.json',encoding='utf8') as f:
            self.preprocessor=json.load(f)
        self.global_conf=conf

    def exec(self):
        '''
        运行测试任务

        训练测试的指标：训练花费的时长、训练停止条件的设置、训练结束时达到的准确率、训练时准确率的变化、硬件利用率、损失函数的变化
        '''
        if self.model['type']=='source':
            filename=self.global_conf['source_run_py_dir']+'/'+self.name+'_'+self.model.name+'_'+self.dataset.name+'_'+self.preprocessor.name+'_'+self.content['type']+'.py'
            target_file=build_model(self.model,self.dataset,self.preprocessor,filename,self.content['type'])


if __name__=='__main__':
    from utils.conf import load_prime_config
    conf=load_prime_config
    task={
            "name": "task1",
            "dataset": "THCHS-30",
            "model": "SpeechModel251BN",
            "preprocessor":"data_process1"
        }
    tt=Task(task['name'],task['dataset'],task['model'],task['preprocessor'],conf)
    tt.exec()